package com.hnevc.foodserver.controller;

import com.hnevc.foodserver.bean.UpdatePWDVo;
import com.hnevc.foodserver.bean.UserPageVo;
import com.hnevc.foodserver.bean.Users;
import com.hnevc.foodserver.service.UsersService;
import io.swagger.annotations.*;
import net.bytebuddy.utility.RandomString;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin("*")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    UsersService usersService;
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "密码"),
    })
    public Map<String,Object> login(@RequestBody Users users){
        Map<String,Object> map = new HashMap<String,Object>();
        Users login = usersService.login(users);
        if(login!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",login);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "密码"),
    })
    public Map<String,Object> register(@RequestBody Users users){
        Map<String,Object> map = new HashMap<String,Object>();
        Users register = usersService.register(users);
        if(register!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",register);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping("/upload_headpic")
    @ApiOperation(value = "更新用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "用户头像"),
            @ApiImplicitParam(name = "id",value = "用户ID")
    })
    public Map<String,Object> uploadHeadPic(@RequestParam("file") MultipartFile file,
                                            @RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            byte[] bytes = file.getBytes();
            String filePath =  new RandomString(10).nextString() + file.getOriginalFilename();
            Path path = Paths.get("D://upload//headpic//" +filePath);
            Files.write(path, bytes);
            map.put("success",true);
            map.put("msg","成功");
            //TODO 更新到数据库
            // 1. 获取用户服务对象，
            Users users = usersService.updateHeadPic(id, "headpic/" + filePath);
            map.put("data",users);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","文件读取错误");
        }
        return map;
    }
    @PostMapping("/list")
    @ApiOperation(value = "查询用户信息")

    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Users> users= usersService.list();
        if(users!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",users);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping("/listByPage")
    @ApiOperation(value = "分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex",value = "页码从0开始"),
            @ApiImplicitParam(name = "pageSize",value = "每页记录条数")
    })
    public Map<String,Object> listByPage(@RequestBody UserPageVo userPageVo){
        Map<String,Object> map = new HashMap<String,Object>();
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        PageRequest pageRequest = PageRequest.of(userPageVo.getPageIndex(),userPageVo.getPageSize(),sort);
        Page<Users> users= usersService.listByPage(pageRequest);
        if(users!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",users);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "ID"),
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "nickname",value = "昵称"),
            @ApiImplicitParam(name = "password",value = "密码"),
            @ApiImplicitParam(name = "telephone",value = "电话"),
            @ApiImplicitParam(name = "icon",value = "头像")
    })
    public Map<String,Object> update(@RequestBody Users users){
        Map<String,Object> map = new HashMap<String,Object>();
        Users updateUser = usersService.update(users);
        if(updateUser!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",updateUser);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping("/updatepwd")
    @ApiOperation(value = "更新密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "旧密码"),
            @ApiImplicitParam(name = "newPassword",value = "新密码")
    })
    public Map<String,Object> updatepwd(@RequestBody UpdatePWDVo updatePWDVo){
        Map<String,Object> map = new HashMap<String,Object>();
        Users updateUser = usersService.updatepwd(updatePWDVo);
        if(updateUser!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",updateUser);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}
